package com.product.system.aop;

import cn.hutool.json.JSONUtil;
import com.product.system.domain.SysOperLog;
import com.product.system.service.SysOperLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Aspect
@Component
public class MenuAddLogAop {

    @Autowired
    private SysOperLogService operLogService;

    @Autowired
    private HttpServletRequest request;


    @Around("execution( * com.product.system.controller.SysMenuController.*(..) )")
    public Object menuAddLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        String arg = JSONUtil.toJsonStr(args);
        String url = request.getRequestURI();
        String name = joinPoint.getClass().getName();
        Class<? extends ProceedingJoinPoint> aClass = joinPoint.getClass();
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            if (method.getName().equals("addMenu")){
                SysOperLog sysOperLog = SysOperLog.builder()
                        .title("菜单添加")
                        .method(name + "." + method.getName())
                        .requestMethod(request.getMethod())
                        .operatorType(0)
                        .operUrl(url)
                        .operName(request.getRemoteUser())
                        .operIp(request.getRemoteAddr())
                        .operLocation(request.getRemoteHost())
                       .operParam(arg)
                        .jsonResult(JSONUtil.toJsonStr(args))
                        .build();
                int i = operLogService.addOperLog(sysOperLog);
            }
        }
        return joinPoint.proceed();
    }
}
